/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.XPath;
using System.ComponentModel.Design;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.TypeConverters.BuiltIn
{
	internal sealed class ForEachComponentTypeConverter : SandcastleBuildComponentManagerTypeConverter, ICompositeBuildComponentManager
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		private SandcastleBuildComponentManager manager;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ForEachComponentTypeConverter" /> class.
		/// </summary>
		public ForEachComponentTypeConverter()
		{
		}
		#endregion

		#region Methods
		public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
		{
			SandcastleBuildComponent component = value as SandcastleBuildComponent;

			if (context != null && component != null)
			{
				EnsureManager(component);

				return base.GetProperties(context, manager, attributes);
			}

			return new PropertyDescriptorCollection(null);
		}

		private void EnsureManager(SandcastleBuildComponent component)
		{
			SandcastleBuildComponentManager parent = component.Manager;

			XPathNavigator navigator = component.Configuration.CreateNavigator();

			if (navigator.MoveToChild("components", ""))
			{
				if (manager == null)
					manager = new SandcastleBuildComponentManager(component.LineNumber, component, parent, navigator.CreateNavigator());
			}
			else
			{
				navigator.PrependChildElement(null, "components", null, null);
				navigator.MoveToChild(XPathNodeType.Element);

				manager = new SandcastleBuildComponentManager(component.LineNumber, component, parent, navigator.CreateNavigator());
			}
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				IXPathNavigable navigable = value as IXPathNavigable;

				if (navigable != null)
				{
					XPathNavigator navigator = navigable.CreateNavigator();

					if (navigator.MoveToChild("variable", ""))
						return navigator.GetAttribute("expression", "");
				}

				return string.Empty;
			}
			else
				return base.ConvertTo(context, culture, value, destinationType);
		}
		#endregion

		#region ICompositeBuildComponentManager Members
		public SandcastleBuildComponentManager GetManager(ITypeDescriptorContext context, SandcastleBuildComponent component)
		{
			if (component != null)
				EnsureManager(component);
			
			return manager;
		}
		#endregion
	}
}
